
\documentclass[a4paper,12pt]{article}

% ------
% Fonts and typesetting settings
\usepackage[utf8]{inputenc}


% ------
% Maketitle metadata
\title{
	 Replication: \\ A matter of representation: spatial voting and inconsistent policy preferences 
    \vspace{3mm}
	}
\author{
	 \large
	 \textsc{Lukas F. Stoetzer}
            \thanks{lukas.stoetzer@uzh.ch}
	}
\date{}



 
%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\maketitle

<<include=FALSE>>=
require(MASS)
require(ggplot2)
require(reshape2)

# load Results
load("results_anes.Rdata")
@ 


<<par_est_pap,echo=FALSE,fig.width=7, warnings=F,out.width='\\textwidth', fig.height=3,fig.cap="Parameter estimates for policy-weighted model using perceived (Black) and mean distance (Grey) to candidate platform. Figure shows point estimates  as well as 95\\% and 99\\% confidence intervals for the direct effect of policy distance and interaction effect with inconsistency.",fig.align="center",fig.center=T>>=

par     <- c(coef(m21)[c("DISSfamean","DISSfamean:resid")],
                 coef(m11)[c("DISSfa","DISSfa:resid")])

se     <- c(sqrt(diag(vcov(m21)))[c("DISSfamean","DISSfamean:resid")],
                 sqrt(diag(vcov(m11)))[c("DISSfa","DISSfa:resid")])


res.mw <- data.frame(cbind(1:2,par,se, sort(rep(1:2,2))))
names(res.mw) <- c("par","est","se","model")

# 
res.mw$par <- as.factor(res.mw$par)
levels(res.mw$par) <- c("Policy distance", "Interaction")

# 
res.mw$model <- as.factor(res.mw$model)
levels(res.mw$model) <- c("Perceived distance", " Mean distance" )


ggplot(data=res.mw)  + 
  geom_linerange(aes(x=1, y=est, ymin=est-1.96*se, ymax=est+1.96*se, col=model ),size=2,position=position_dodge(0.4))  +
  geom_linerange(aes(x=1, y=est, ymin=est-2.6*se, ymax=est+2.6*se, col=model ),size=1.6,position=position_dodge(0.4))  +
  geom_point(aes(x=1, y=est,ymin=est-2.6*se, ymax=est+2.6*se, col=model),position=position_dodge(0.4),size=4) +
  coord_flip() + facet_grid(par ~.) +
  geom_hline(yintercept=0,alpha=0.3) + 
  scale_x_continuous(name="", labels="" , breaks=1,limits=c(0.8,1.3)) +
  scale_y_continuous(name="",limits=c(-0.1,0.9)) +
  theme_bw() + 
  theme( strip.text.y = element_text(size=10,angle=360)) + 
  #scale_y_continuous(name="Estimate",limits=c(-0.5,4))+ 
  scale_color_grey() +
  theme(legend.direction = "horizontal", legend.position = "top", 
        #panel.grid.major = element_blank(),
        #panel.grid.minor = element_blank(),
        panel.background = element_blank()
        ,text=element_text(family="Palatino", size=11)
        ) +
  guides(col = guide_legend(nrow = 1,byrow=T,title = ""))


@

<<me_wm,include=T,echo=FALSE,fig.width=7, out.width='0.9\\textwidth', fig.height=4,fig.cap="Marginal effect of policy distance on difference in expected utility for varying levels of inconsistency. The left panel shows the marginal effect for mean candidate positions, the right panel for perceived candidate distance. The lines on the x-axis show the distribution of inconsistency in the sample.",fig.center=T>>=


# FAmean
S <- mvrnorm(10000,coef(m21),vcov(m21))
qresid <- quantile(m21$data$resid,seq(0,1,0.01),na.rm=T)

res <- sapply(qresid, function(x) 
                      quantile(  S[,"DISSfamean"] + S[,"DISSfamean:resid"]*x,c(.975,0.5,0.025)))
res <- as.data.frame(t(res))
names(res) <- c("high","mid","low")
res$qresid <- qresid
res$model <- 1

# FA
S <- mvrnorm(10000,coef(m11),vcov(m11))
qresid <- quantile(m11$data$resid,seq(0,1,0.01),na.rm=T)

res2 <- sapply(qresid, function(x) 
                      quantile(  S[,"DISSfa"] + S[,"DISSfa:resid"]*x,c(.975,0.5,0.025)))
res2 <- as.data.frame(t(res2))
names(res2) <- c("high","mid","low")
res2$qresid <- qresid
res2$model <- 2

res <- rbind(res,res2)
res$model <- factor(res$model) ; levels(res$model) <- c( " Mean Candidate Position","Perceived Candidate Position" )
ggplot() + geom_line(data=res,aes(x=qresid,y=mid,ymin=low,ymax=high)) +
  geom_ribbon(data=res,aes(x=qresid,y=mid,ymin=low,ymax=high),alpha=0.1)  +
  theme_bw() + ylab("Marginal effect policy distance on expected utility") + 
  xlab("Inconsistency") + facet_grid(~model) +
  theme(text=element_text(family="Palatino", size=11))  + 
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank()
        ,text=element_text(family="Palatino", size=11)
  ) + 
  geom_rug(data=m11$data,aes(x=resid)) + 
  geom_hline(yintercept=0, alpha=0.4)


@

<<"sim",echo=FALSE,include=T,fig.width=7, out.width='0.9\\textwidth', fig.height=4,fig.cap="Probability to vote for Obama by policy distance to Obama minus distance to McCain simulated from mean platform model. The left panel shows the voting probabilities for a respondent with consistent policy preferences (value correspondent to the median value in the sample). The right panel shows the voting probabilities for respondent with inconsistent policy preferences (value correspondent to the 95 \\% quantile in the sample). ">>=

c <- coef(m21)
V <- vcov(m21)

# Sampling distribution
S <- mvrnorm(10000,c,V)


# Get Covariates for controls
means <- apply(m21$data,2,median,na.rm=TRUE)
means <- means[names(c)[4:9]]

dis <- seq(min(m21$data$DISSfamean,na.rm=TRUE),max(m21$data$DISSfamean,na.rm=TRUE),1)
means <- matrix(c(means),nrow=length(dis),ncol=length(means), byrow=TRUE)

# for sd=0,1,2
quantiles <- c(0.5,0.95)
sd <- quantile(m21$data$resid,na.rm=TRUE,quantiles)


X <- cbind(1,dis,sd[1],means,dis*sd[1])

for (i in 2:length(sd)){
X <- rbind(X,cbind(1,dis,sd[i],means,dis*sd[i]))
}


# Get values
S1 <- pnorm(X%*% t(S))


# Gte Info in Data. frame
df <- as.data.frame(t(apply(S1,1,quantile,c(0.025,0.5,0.975))))


names(df) <- c("low","mid","high")
df$Dis <- dis
df$sd <- as.factor(X[,3])
levels(df$sd) <- c("Median Inconsistency", "95% Quantile of Inconsistency")


ggplot(df,aes(x=Dis,y=mid,ymin=low,ymax=high)) + geom_vline(xintercept=0,alpha=0.1) + geom_line() + geom_ribbon(alpha=0.2)  +
  theme_bw() + ylab("Probability to vote for Obama in 2012 Election") + 
  xlab("Policy distance to Obama minus distance to McCain") + 
  theme(legend.position="top",text=element_text(family="Palatino", size=11))  + 
  facet_wrap(~sd) +
  theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank()
      ,text=element_text(family="Palatino", size=11)
        )

@ 


<<"eD",echo=FALSE,include=T,fig.width=7, out.width='\\textwidth', fig.height=4,fig.cap="Average expected distance for varying degrees of inconsistency. The x-axis shows the expected distance to a respondent's representative. Values of zero imply that a voter is expected to share the same position with the candidate she intends to vote for. Decreasing values indicate a stronger expected distance.",fig.center=T>>=

  sel <- na.action(m21)
  DISS.PB <- -(dta$ML1[-sel] -mean(dta$ML1.1,na.rm=T))^2
  DISS.PJ <- -(dta$ML1[-sel]- mean(dta$ML1.2,na.rm=T))^2

  eD <- function(x,model,Diss){
    
    require(MASS)
    
    X <-    model.matrix(model)
    X[,c("resid","DISSfamean:resid")] <- cbind(x,x*X[,"DISSfamean"])
    
    S <- mvrnorm(10000,coef(model),vcov(model))
    
    p1 <- pnorm(X%*%t(S))
    mED <- function(x) mean(x*Diss[,1]+ (1-x)*Diss[,2])
  
    return(apply(p1,2,mED))
    
  }
  
  x1 <- quantile(dta$resid,seq(0.1,0.9,length.out=3),na.rm=T)
  
  res <- sapply(x1, eD, model=m21,Diss=cbind(DISS.PB,DISS.PJ))
  

df <- as.data.frame(melt(res))
names(df) <- c("id","quant","value")

ggplot(df,aes(x=value, fill=quant)) + geom_density(alpha=0.2) +
  theme_bw() + ylab("Density") + 
  xlab("Expected representational closeness") +  xlim(limits=c(-0.9,0)) + 
  theme(legend.position="top",text=element_text(family="Palatino", size=11))  + 
  theme(panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank()
      ,text=element_text(family="Palatino", size=11)
        ) + scale_fill_grey(start = 1, end = 0) +
    labs(fill="Inconsistency quantile")
@

\end{document}